package main

import "fmt"

//闭包函数
/**
	闭包函数
	由自由变量、局部变量、函数体组成
 */
func adder() func(int) int {
	sum := 0
	return func(i int) int {
		sum += i
		return sum
	}
}
//正统函数式编程
//递归
type iAdder func(int) (int, iAdder)

func adder2(base int) iAdder {
	return func(v int) (int, iAdder) {
		return base + v, adder2(base + v)
	}
}
func main() {
	//a := adder()
	a := adder2(2)

	for i := 0; i < 10; i++ {
		//fmt.Println(a(i))
		var s int
		s, a = a(i)
		fmt.Printf("0 + 1 + ... + %d = %d\n", i, s)
	}
}
